package cn.kow.kmall.controller.order;

import cn.kow.kmall.admin.pojo.vo.OrderItemVO;
import cn.kow.kmall.admin.pojo.vo.OrderVO;
import cn.kow.kmall.common.enums.OrderStatusEnum;
import cn.kow.kmall.controller.BaseController;
import cn.kow.kmall.core.response.KmallResult;
import cn.kow.kmall.core.util.MoneyUtils;
import cn.kow.kmall.pojo.dto.OrderQueryDTO;
import cn.kow.kmall.pojo.entity.Order;
import cn.kow.kmall.pojo.entity.OrderItem;
import cn.kow.kmall.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2020-04-28 16:35
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @PostMapping(value = "/list")
    public KmallResult list(@RequestBody OrderQueryDTO queryDTO) {
        String consignee = queryDTO.getConsignee();
        if (StringUtils.isNotBlank(consignee)) {
        }
        Page<Order> page = orderService.listByPage(queryDTO);
        List<OrderVO> voList = new ArrayList<>();
        if (page.hasContent()) {
            List<Order> orderList = page.getContent();
            for (Order order : orderList) {
                voList.add(toOrderVO(order));
            }
        }

        return KmallResult.success().put("rows", voList)
                .put("total", page.getTotalElements());
    }

    @GetMapping(value = "/{orderNo}")
    public KmallResult info(@PathVariable String orderNo) {
        Order order = orderService.getOrder(orderNo);
        List<OrderItem> itemList = orderService.listOrderItem(orderNo);
        List<OrderItemVO> itemVOList = itemList.stream().map(i -> toOrderItemVO(i)).collect(Collectors.toList());
        OrderVO orderVO = toOrderVO(order);
        orderVO.setItemList(itemVOList);
        return KmallResult.success(orderVO);
    }

    @PostMapping(value = "/{orderNo}/send")
    public KmallResult info(@PathVariable String orderNo, @RequestBody Map<String, String> paramMap) {
        String logisticsNo = paramMap.get("logisticsNo");
        Order order = orderService.getOrder(orderNo);
        if (!OrderStatusEnum.PAYED.getValue().equals(order.getOrderStatus())) {
            return KmallResult.fail("发货失败，订单状态错误");
        }
        order.setUpdateBy(currentUser().getUsername());
        order.setLogisticsNo(logisticsNo);
        order.setOrderStatus(OrderStatusEnum.SEND.getValue());
        orderService.updateOrder(order);
        return KmallResult.success();
    }

    private OrderVO toOrderVO(Order order) {
        OrderVO vo = new OrderVO();
        BeanUtils.copyProperties(order, vo);
        vo.setTotalAmount(MoneyUtils.Fen2YuanStr(order.getTotalFee()));
        vo.setCouponAmount(MoneyUtils.Fen2YuanStr(order.getCouponFee()));
        vo.setDiscountAmount(MoneyUtils.Fen2YuanStr(order.getDiscountFee()));
        vo.setFreight(MoneyUtils.Fen2YuanStr(order.getPostFee()));
        vo.setPayAmount(MoneyUtils.Fen2YuanStr(order.getPayFee()));
        vo.setConsignee(order.getReceiverName());
        vo.setPhone(order.getReceiverPhone());
        vo.setAddress(order.getReceiverAddress());
        return vo;
    }

    private OrderItemVO toOrderItemVO(OrderItem orderItem) {
        OrderItemVO vo = new OrderItemVO();
        BeanUtils.copyProperties(orderItem, vo);
        vo.setUnitPrice(MoneyUtils.Fen2YuanStr(orderItem.getUnitPrice()));
        vo.setTotalPrice(MoneyUtils.Fen2YuanStr(orderItem.getTotalFee()));
        vo.setPayAmount(MoneyUtils.Fen2YuanStr(orderItem.getPayFee()));
        return vo;
    }
}
